package com.sx.art2.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sx.art2.beans.Art;
import com.sx.art2.beans.AuctionInfo;
import com.sx.art2.service.AuctionInfoService;
import com.sx.art2.tools.Page;

@Controller
@RequestMapping("/AuctionInfo")
public class AuctionInfoController {
	
	@Autowired
	AuctionInfoService auctionInfoService;
	
	@RequestMapping("/list")
	public ModelAndView list(Page page) {
		
		ModelAndView mv=new ModelAndView();
		page.setPageSize(12);
		page.setTotalCount(auctionInfoService.getTotalCount());
		List<AuctionInfo> auctionInfoList=auctionInfoService.list(page,1);
		mv.addObject("page", page);
		mv.addObject("auctionInfoList", auctionInfoList);
		mv.setViewName("mall2");
		return mv;
	}
	@RequestMapping("/selectAllArts")
	public ModelAndView selectAllArts(Art art) {
		ModelAndView mv=new ModelAndView();		
		List<AuctionInfo> likeArts=auctionInfoService.selectAll(art);	
		mv.addObject("auctionInfoList", likeArts);
		System.out.println(likeArts);
		mv.setViewName("mall2");
		return mv;
	}
	@RequestMapping("/selectLikeAuction")
	public ModelAndView selectLikeAuction(Integer categoryId,String artSize, String auctionNowPrice) {
		ModelAndView mv=new ModelAndView();
		List<AuctionInfo> LikeAuctions=auctionInfoService.selectLikeAuction(categoryId,artSize,auctionNowPrice);
		mv.addObject("auctionInfoList", LikeAuctions);
		System.out.println(LikeAuctions);
		mv.setViewName("mall2");
		return mv;
	}
	@RequestMapping("/selectLikePreviewAuction")
	public ModelAndView selectLikePreviewAuction(Integer categoryId,String artSize, String auctionNowPrice) {
		ModelAndView mv=new ModelAndView();
		List<AuctionInfo> LikeAuctions=auctionInfoService.selectLikePreviewAuction(categoryId, artSize, auctionNowPrice);
		mv.addObject("auctionInfoList", LikeAuctions);
		System.out.println(LikeAuctions);
		mv.setViewName("preview");
		return mv;
	}
	
	@RequestMapping("/list2")
	public ModelAndView list2(Page page) {
		
		ModelAndView mv=new ModelAndView();
		page.setPageSize(12);
		page.setTotalCount(auctionInfoService.getTotalCount());
		List<AuctionInfo> auctionInfoList=auctionInfoService.list(page,0);
		mv.addObject("page", page);
		mv.addObject("auctionInfoList", auctionInfoList);
		mv.setViewName("preview");
		return mv;
	}
	@RequestMapping("/list3")
	public ModelAndView list3(Page page) {
		
		ModelAndView mv=new ModelAndView();
		page.setPageSize(12);
		page.setTotalCount(auctionInfoService.getTotalCount());
		List<AuctionInfo> auctionInfoList=auctionInfoService.success(page);
		mv.addObject("page", page);
		mv.addObject("auctionInfoList", auctionInfoList);
		mv.setViewName("success");
		return mv;
	}
	@RequestMapping("/lookArt")
	public ModelAndView lookArt(int auctionInfoId,int sign) {
		ModelAndView mv=new ModelAndView();
		AuctionInfo auctionInfo=auctionInfoService.lookArt(auctionInfoId);
		mv.addObject("auctionInfo", auctionInfo);
		if(sign==0)
			   mv.setViewName("pro_detail");
		else if(sign==1)
			mv.setViewName("pro_detail");
		else if(sign==2)
			mv.setViewName("pro_detail");
		return mv;
	}
	

	@RequestMapping("/getAuctionInfoById")
	public ModelAndView getAuctionInfoById(int auctionInfoId) {
		ModelAndView mv=new ModelAndView();
		AuctionInfo AuctionInfo=auctionInfoService.selectAuctionInfoById(auctionInfoId);
		mv.addObject("AuctionInfo", AuctionInfo);
		mv.setViewName("admin/auctionInfo-update");
		return mv;
	}
	@RequestMapping("/update")
	
	public String update(AuctionInfo auctionInfo) {
		String date="success";		
		try {
			auctionInfoService.update(auctionInfo);
		} catch (Exception e) {
			// TODO: handle exception
			date="error";
		}
		return date;
	}
	@RequestMapping("/preview")
	public ModelAndView preview() {
		ModelAndView mv=new ModelAndView();
		List<AuctionInfo> auctionInfoList=auctionInfoService.list(0);
		mv.addObject("auctionInfoList", auctionInfoList);
		mv.setViewName("admin/preview");
		return mv;
	}
	@RequestMapping("/auctioning")
	public ModelAndView auctioning() {
		ModelAndView mv=new ModelAndView();		
		List<AuctionInfo> auctionInfoList=auctionInfoService.list(1);
		mv.addObject("auctionInfoList", auctionInfoList);
		mv.setViewName("admin/auctioning");
		return mv;
	}
	@RequestMapping("/auctionSuccess")
	public ModelAndView auctionSuccess() {
		ModelAndView mv=new ModelAndView();
		List<AuctionInfo> auctionInfoList=auctionInfoService.list(2);
		mv.addObject("auctionInfoList", auctionInfoList);
		mv.setViewName("admin/auctionSuccess");
		return mv;
	}
	@RequestMapping("/auctionEnd")
	public ModelAndView auctionEnd() {
		ModelAndView mv=new ModelAndView();
		List<AuctionInfo> auctionInfoList=auctionInfoService.list(2);
		mv.addObject("auctionInfoList", auctionInfoList);
		mv.setViewName("admin/auctionEnd");
		return mv;
	}
	@RequestMapping("/chepai")
	@ResponseBody
	public String chepai(int auctionInfoId) {
		String date="success";
		auctionInfoService.updateState(auctionInfoId, 4);
		return date;
	}
	@RequestMapping("/getAuctionInfo")
	public ModelAndView getAuctionInfo(int auctionInfoId) {
		ModelAndView mv=new ModelAndView();
		AuctionInfo auctionInfo=auctionInfoService.selectAuctionInfoById(auctionInfoId);
		mv.addObject("auctionInfo", auctionInfo);
		mv.setViewName("admin/auctionInfo_detailed");
		return mv;
	}
}
